
cd "C:\Users\patri\Dropbox\Slantchev Revised\Empirical Work\LeaderDefault\Data\"

********************************************
**Number of leaders that defaulted (mentioned in Intro)

use "leaderdefault_041018.dta", clear

set more off

xtset ccode year

drop if year<1875

sort leadid1 year 

by leadid1: egen sum_onset = total(ext_onset2) if leadid1!=. & ext_onset2!=. 

cap drop dup

sort leadid1
quietly by leadid1:  gen dup = cond(_N==1,0,_n)
drop if dup>1

gen sum2 = sum_onset
replace sum2 = 1 if sum_onset>1 &sum_onset!=.

tab sum2


**********************************************************
**List of Irregular Leaders and Default (Table B in Appendix)

use "leaderdefault_041018.dta", clear

set more off

**ssc install listtex
listtex  country year first second if ext_onset2==1 & (lirr_entry==1 | irr_entry==1), ///
type  end(\\) missnum(NA)  head("\begin{tabular}{l l l l}" ///
            `"\textit{Country}&\textit{Year of Default}&\textit{Irregular $\Delta$ in time t}&\textit{Irregular $\Delta$ in time t-1} \\"') foot("\end{tabular}")


**Cross Tabs			
tab ext_onset2 lirr_entry, col row chi2

**Proportions test
prtest ext_onset2, by(lirr_entry)


**Cross Tabs (regular leader)			
tab ext_onset2 lreg_leader, col row chi2

**Proportions test (regular leader)	
prtest ext_onset2, by(lreg_leader)

*****************************************************************************8
***Figure 1: Leadership Change and Default
use "leaderdefault_041018.dta", clear

set more off
xtset ccode year


gen leader_group = 0 if l.irr_entry==0 & l.reg_leader==0
replace leader_group = 1 if l.irr_entry==0 & l.reg_leader==1
replace leader_group = 2 if l.irr_entry==1 & l.reg_leader==0

cibar ext_onset2 , over1(leader_group)  level(95) bargap(10)  barcol( emidblue emidblue*1.5 emidblue*2.5 )  graphopts( ysc(r(0 (.01) .05)) ///
  ytitle("Percentage of Default Onset") ///
  xlabel(1 "No Leader Change" 2.20 "Regular Leader Change" 3.3 "Irregular Leader Change") xtitle("") ///
 ylabel(0 (.05) .16) ///
name(m2, replace)  legend(off)   graphregion(color(white))  ) 

*****************************************************************************8
***Table 1: Leadership Change and Democracy
use "leaderdefault_041018.dta", clear

set more off
xtset ccode year
gen lead_change = 1 if irr_entry==1
replace lead_change = 2 if reg_leader==1
replace lead_change = 0 if reg_leader==0 & irr_entry==0


tab dem2 lead_change, col row chi2 

*****************************************************************************8
***Table 2: Random Effects Logit Examining Leader Change and Default Onset, 1875-2015
use "leaderdefault_041018.dta", clear

set more off
xtset ccode year
*Model 1

xtlogit F.ext_onset2 irr_entry reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
estimates store re1

*Model 1 with odds ratio
xtlogit F.ext_onset2 irr_entry reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2 , i(ccode) or

 
*Model 2 Regional fixed effects 
 tab region, gen(region_)
 
xtlogit F.ext_onset2 irr_entry reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2 region_*, i(ccode)
estimates store re2


*Model 3 Year Fixed effects
xtlogit F.ext_onset2 irr_entry reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2 i.year, i(ccode)
estimates store re3
 
 
*Model 4 Interaction model 
xtlogit F.ext_onset2 max_irregular ten_irr2   lnten2   ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch  avg_ukus dem2, i(ccode)
 estimates store re4
 
 
 
 
 *Model with interaction with regular leader
 gen ten_reg2 = lnten2*max_regular

xtlogit F.ext_onset2 max_regular ten_reg2  lnten2   ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch  avg_ukus dem2 , i(ccode) 

*Setup for Model 5 - Deriving Pr(instability
logit irr_entry  lnten2 lngdppcMAD    ///
 iyrs i2 i3 reg_coups  lngdpMAD ///
 egrowth dem2 i.region coldwar postcoldwar, cluster(ccode)
 
 lroc

 cap drop pr_irr
predict pr_irr

*Model 5 - Includes Pr((Irregular Change))
 xtlogit F.ext_onset2 irr_entry pr_irr   reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis debtGDP debtch avg_ukus  , i(ccode)
 estimates store pr1

set seed 654321

gen Fext_onset2 = F.ext_onset2

*Model 5 - Includes Pr((Irregular Change)) and has bootstrapped SE's

bootstrap,   reps(500)   strata(ccode): logit Fext_onset2 irr_entry pr_irr   reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis debtGDP debtch avg_ukus
  estimates store pr2


 
lab var irr_entry "Irregular Leader Change" 
 lab var reg_leader "Regular Leader Change" 
 lab var max_irregular "Irregular Leader" 
 lab var max_regular "Regular Leader" 
 lab var ten_irr2  "Tenure*Irregular Leader" 
  lab var ten_reg2  "Tenure*Regular Leader" 
 lab var  oyrs "Years since Default Onset"
 lab  var reg_all "\# of Defaults in Region"
 lab var systemiccrisis "Economic Crisis"
 lab var maddion_gdp "GDP"
 lab var egrowth "Growth"
 lab var debtch "Change in Debt"
 lab var avg_ukus "Global Interest Rate"
 lab var lnten2 "Tenure"
  lab var pr_irr "Pr(Irregular Change)"
 
 
      estout re1 re2 re3 re4 pr2  , cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
	eqlab("/lnsig2u" "ln($\sigma^2_u$)", none) ///
stats( ll aic N, fmt(%9.1f %9.1f %9.0f) labels("Log-Likelihood" "AIC") star)  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(irr_entry reg_leader max_irregular  lnten2 ten_irr2 pr_irr dem2) drop(o2 o3 *year region_* ) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4") ///
title("")

*********
**Figure 2 (Interaction effects)


use "leaderdefault_041018.dta", clear

set more off

xtset ccode year

drop if year<1875



melogit F.ext_onset2 lnten2 max_irregular ten_irr2     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch  avg_ukus dem2 || ccode:
 


 
 sum  oyrs if e(sample), detail
local c4 = r(p50) 
sum o2 if e(sample), detail
local c5 = r(p50)
sum o3 if e(sample), detail
local c6 = r(p50)
sum reg_all if e(sample), detail
local c7 = r(p50)
sum systemiccrisis if e(sample), detail
local c8 = r(p50)
sum maddion_gdp if e(sample), detail
local c9 = r(p50)
sum  egrowth if e(sample), detail
local c10 = r(p50)
sum  debtGDP if e(sample), detail
local c11 = r(p50)
sum  debtch if e(sample), detail
local c12 = r(p50)
sum  avg_ukus if e(sample), detail
local c13 = r(p50)
sum  dem2 if e(sample), detail
local c14 = r(p50)




xtlogit F.ext_onset2 lnten2 max_irregular ten_irr2     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch  avg_ukus dem2 , i(ccode)

drawnorm b1-b16, n(10000) means(e(b)) cov(e(V)) clear

postutil clear

postfile mypost prob_hat0 lo0 hi0 prob_hat1 lo1 hi1 diff_hat diff_lo diff_hi ///
           using "C:\Users\patri\Dropbox\My Projects\sim.dta", replace

			
			noisily display "start"
			
			local a=0
			
			
			while `a' <= 10 { 
  {
     generate x_betahat0  = b1*`a'  + b2*0 + b3*`a'*0   + b4*`c4' + b5*`c5' + b6*`c6' + b7*`c7' + b8*`c8'+ b9*`c9'  ///
 + b10*`c10' + b11*`c11' + b12*`c12' + b13*`c13' + b14*`c14'  
     
gen prob0 = (exp(x_betahat0))/(1 + exp(x_betahat0))
egen probhat0 =mean(prob0)
	 
							
       generate x_betahat1  = b1*`a'  + b2*1 + b3*`a'*1   + b4*`c4' + b5*`c5' + b6*`c6' + b7*`c7' + b8*`c8'+ b9*`c9'  ///
 + b10*`c10' + b11*`c11' + b12*`c12' + b13*`c13' + b14*`c14' 
	   
							
gen prob1= (exp(x_betahat1))/(1 + exp(x_betahat1))
egen probhat1 =mean(prob1)
 	
	gen diff=prob1-prob0
 

egen diffhat=mean(diff)

tempname prob_hat0 lo0 hi0 prob_hat1 lo1 hi1 diff_hat diff_lo diff_hi

_pctile prob0, p(5,95) 
scalar `lo0' = r(r1)
scalar `hi0' = r(r2)

_pctile prob1, p(5,95)
scalar `lo1'= r(r1)
scalar `hi1'= r(r2)

_pctile diff, p(5,95)
scalar `diff_lo'= r(r1)
scalar `diff_hi'= r(r2)

scalar `prob_hat0'=probhat0
scalar `prob_hat1'=probhat1
scalar `diff_hat'=diffhat

 post mypost (`prob_hat0') (`lo0') (`hi0') (`prob_hat1') (`lo1') (`hi1') /// 
                (`diff_hat') (`diff_lo') (`diff_hi')
      }
 
 drop x_betahat0 x_betahat1 prob0 prob1 diff probhat0 probhat1 diffhat
local a=`a'+ .01
display "." _c
} 

display ""

postclose mypost

use "C:\Users\patri\Dropbox\My Projects\sim.dta", clear

gen yline=0

gen MV = (_n-1)/100
drop if MV>10



graph twoway  ///
         ||  line diff_hat MV, clwidth(medium) clcolor(blue) clcolor(black) ///
         ||  line diff_lo MV, clpattern(dash) clwidth(thin) clcolor(black) ///
         ||  line diff_hi MV, clpattern(dash) clwidth(thin) clcolor(black) ///
         ||  ,  yline(0)  xtitle("ln(Leader Tenure)") ytitle("Marginal Effect of Irregular Leader") ///
	legend(off)

		 	
gr_edit style.editstyle boxstyle(shadestyle(color(white))) editcopy
gr_edit style.editstyle boxstyle(linestyle(color(white))) editcopy
		gr_edit legend.plotregion1.label[1].text = {}
gr_edit legend.plotregion1.label[1].text.Arrpush Marginal Effect



******************************************************
***Table 3: Instability Confounders
******************************************************
use "leaderdefault_041018.dta", clear

set more off
xtset ccode year

 
 

 
 xtlogit F.ext_onset2 irr_entry count_protestPRIO reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
 est sto m1
 
  xtlogit F.ext_onset2 irr_entry count_vioPRIO reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
  est sto m2
  
  xtlogit F.ext_onset2 irr_entry count_riotPRIO reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
  est sto m3
 
 xtlogit F.ext_onset2 irr_entry max_civilwar1 reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
  est sto m4
  
   xtlogit F.ext_onset2 irr_entry  conflict reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
  est sto m5
  
     xtlogit F.ext_onset2 irr_entry max_civilwar1 count_protestPRIO count_riotPRIO count_vioPRIO conflict reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
  est sto m6
 

 lab var irr_entry "Irregular Leader Change" 
 lab var reg_leader "Regular Leader Change" 



 lab var  oyrs "Years since Default Onset"
 lab  var reg_all "\# of Defaults in Region"
 lab var systemiccrisis "Economic Crisis"
 lab var maddion_gdp "GDP"
 lab var egrowth "Growth"
 lab var debtch "Change in Debt"
 lab var avg_ukus "Global Interest Rate"
 lab var lnten2 "Tenure"
lab var conflict "Interstate Conflict"
lab var max_civilwar1"Intrastate Conflict"
lab var count_riotPRIO "Riots"
lab var count_protestPRIO "Protests"
lab var count_vioPRIO "Violent Attacks"

 
      estout m1 m2 m3 m4 m5 m6 , cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
	eqlab("/lnsig2u" "ln($\sigma^2_u$)", none) ///
stats( ll aic N, fmt(%9.1f %9.1f %9.0f) labels("Log-Likelihood" "AIC"))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(irr_entry reg_leader count_protestPRIO count_riotPRIO count_vioPRIO max_civilwar1 conflict) drop(o2 o3) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4") ///
title("")

**Interacting the confounder (not shown in manuscript) 
 xtlogit F.ext_onset2 irr_entry##max_civilwar1 reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
 
 xtlogit F.ext_onset2 irr_entry##conflict reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
 
 xtlogit F.ext_onset2 irr_entry##c.count_protestPRIO reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
 
  xtlogit F.ext_onset2 irr_entry##c.count_vioPRIO reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
 
  xtlogit F.ext_onset2 irr_entry##c.count_riotPRIO reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
 

*****************************************************************
*****Table 4  Random Effects Logit Examining Leader Experience


use "leaderdefault_041018.dta", clear
xtset ccode year


xtlogit F.ext_onset2 irr_entry mmilservice mcombat mrebel meconomics mmilitarycareer myrsexper reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
estimates store re1



xtlogit F.ext_onset2 irr_entry mmilservice  reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
estimates store re2


xtlogit F.ext_onset2 irr_entry mcombat  reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
estimates store re3


xtlogit F.ext_onset2 irr_entry mrebel  reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
estimates store re4


xtlogit F.ext_onset2 irr_entry mmilitarycareer  reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
estimates store re5


xtlogit F.ext_onset2 irr_entry myrsexper  reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
estimates store re6


      estout re1 re2 re3 re4 re5 re6, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
	eqlab("/lnsig2u" "ln($\sigma^2_u$)", none) ///
stats( ll aic N, fmt(%9.1f %9.1f %9.0f) labels("Log-Likelihood" "AIC") star)  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(irr_entry mmilservice mcombat mrebel meconomics mmilitarycareer myrsexper) drop(o2 o3  ) ///
mlabels ("Model 1" "Model 2" "Model 3" ) ///
title("")

****************************************************************************************************
*******Matching Table 5

use "leaderdefault_041018.dta", clear

set more off


 xtset ccode year


gen Fext_onset2 = F.ext_onset2

cap drop _m

**ssc install nnmatch
 nnmatch Fext_onset2 irr_entry    ///
 oyrs o2 o3 reg_all  maddion_gdp ///
 egrowth debtGDP debtch avg_ukus  ///
  , tc(att) bias(bias) m(4) level(95)  exact(dem2 systemiccrisis) keep(bavg2) replace
  
  use bavg2, clear
  
  

  gen delta = Fext_onset2_1-Fext_onset2_0 

  **ssc install rbounds
     rbounds delta, gamma(1 (.1) 3.5)  
	 
	 
******************************************************************************************
********Table 6 Difference in Entry

use "leaderdefault_041018.dta", clear
xtset ccode year

gen uncoord2 = uncoord 
replace uncoord2 = 0 if irr_entry ==0 & year >=1950

gen coord2 = coord 
replace coord2 = 0 if irr_entry ==0   & year >=1950

replace uncoord2 = 0 if uncoord2==1 & coord2==1




xtlogit F.ext_onset2    coord2 uncoord2 reg_leader    ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus if dem2==0, i(ccode)
estimates store re1

lincom _b[uncoord2] - _b[coord2] 
test _b[uncoord2] - _b[coord2] =0

lincom _b[reg_leader ] - _b[coord2] 
test _b[reg_leader ] - _b[coord2] =0

lincom _b[uncoord2] - _b[reg_leader] 
test _b[uncoord2] - _b[reg_leader] =0




lab var uncoord2 "Shuffling Change"
lab var coord2 "Outsider  Change"
 lab var reg_leader "Regular Leader Change" 


      estout re1, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
	eqlab("/lnsig2u" "ln($\sigma^2_u$)", none) ///
stats( ll aic N, fmt(%9.1f %9.1f %9.0f) labels("Log-Likelihood" "AIC") star)  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order( uncoord2 coord2  reg_leader) drop(oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus  ) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4") ///
title("")






***Appendix*******************
*******************************

cd "C:\Users\Dr. Shea\Dropbox\Slantchev Revised\Empirical Work\LeaderDefault\Data\"


use "leaderdefault_041018.dta", clear

set more off

xtset ccode year

**Summary Table
sum ext_onset2 irr_entry reg_leader reg_all  systemiccrisis maddion_gdp ///
egrowth debtGDP debtch oyrs o2 o3 polity2 avg_ukus



**Separation Plot code for R
/*

rm(list=ls())

#Load Library
library(foreign)
library(plm)
library(pglm)
library(MASS)

#install.packages("readstata13")

library(readstata13)

data2<-read.dta13("leaderdefault_041018.dta")



model2<-glm(irr_entry~ log(mad_pc2) +
                dem2 +log(tenure)
             + iyrs+i2+i3+reg_coups+ log(maddion_gdp) 
            +egrowth+factor(region)+coldwar+postcoldwar
            ,  family= binomial(logit) , data=data2)


summary(model2)

library(separationplot)

separationplot(pred=model2$fitted.values, actual=model2$y, type="rect",
               line=TRUE, show.expected=TRUE,  width = 9, height = 1.2, heading="Probability of Irregular Leader Change", zerosfirst)

			   */
			   
			   
*******
**Economic Instability

use "leaderdefault_041018.dta", clear
xtset ccode year

xtlogit F.ext_onset2 irr_entry reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2 if  egrowth <0.00, i(ccode)
estimates store re1


generate moveave1 = ( egrowth + L1.egrowth) / 2
generate moveave3 = (F1.egrowth + egrowth + L1.egrowth) / 3


xtlogit F.ext_onset2 irr_entry reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2 if  moveave1 <0, i(ccode)
estimates store re2


xtlogit F.ext_onset2 irr_entry reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2 if  moveave3 <0, i(ccode)
estimates store re3



xtlogit F.ext_onset2 irr_entry reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2 if  debtch >.3, i(ccode)
estimates store de3


generate debtave1 = ( debtch + L1.debtch) / 2
generate debtave2 = (F1.debtch + debtch + L1.debtch) / 3


xtlogit F.ext_onset2 irr_entry reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2 if  debtave1 >.3, i(ccode)
estimates store de4




xtlogit F.ext_onset2 irr_entry reg_leader     ///
 oyrs o2 o3 reg_all   maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2 if   l.systemiccrisis==1, i(ccode)
estimates store de5







lab var irr_entry "Irregular Leader Change" 
 lab var reg_leader "Regular Leader Change" 
 lab var max_irregular "Irregular Leader" 
 lab var max_regular "Regular Leader" 

 lab var  oyrs "Years since Default Onset"
 lab  var reg_all "\# of Defaults in Region"
 lab var systemiccrisis "Economic Crisis"
 lab var maddion_gdp "GDP"
 lab var egrowth "Growth"
 lab var debtch "Change in Debt"
 lab var avg_ukus "Global Interest Rate"
 

 
      estout re1 re2 de3 de4 de5 , cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
	eqlab("/lnsig2u" "ln($\sigma^2_u$)", none) ///
stats( ll aic N, fmt(%9.1f %9.1f %9.0f) labels("Log-Likelihood" "AIC") star)  style(tex) ///
legend label collabels(none) varlabels(_cons Constant lnsig2u "ln($\sigma^2_u$)") ///
order(irr_entry pr_irr reg_leader dem2) drop(o2 o3  ) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4") ///
title("")



******************************************************
*** Heckman Probit, 1875-2015
******************************************************
use "leaderdefault_041018.dta", clear
xtset ccode year

heckprob F.ext_onset2 irr_entry reg_leader    ///
 reg_all avg_ukus dem2 , sel(F.data = irr_entry reg_leader     ///
   reg_all ///
 avg_ukus dem2 data_region )  vce(cluster ccode)
  estimates store a1
  
  predict p1 if e(sample)
  
  gen irr1 = p1 if irr_entry==1
    gen irr0 = p1 if irr_entry==0

  sum irr1 irr0
  
   heckprob F.ext_onset2 irr_entry reg_leader    ///
 reg_all avg_ukus dem2  lngdpMAD egrowth, sel(F.data = irr_entry reg_leader     ///
   reg_all ///
 avg_ukus dem2 data_region  lngdpMAD egrowth )  vce(cluster ccode)
  estimates store a2
  

  
  lab var irr_entry "Irregular Leader Change" 
 lab var reg_leader "Regular Leader Change" 

 lab var  oyrs "Years since Default Onset"
 lab  var reg_all "\# of Defaults in Region"
 lab var systemiccrisis "Economic Crisis"
 lab var maddion_gdp "GDP"
 lab var lngdpMAD "ln(GDP)
 lab var egrowth "Growth"
 lab var debtch "Change in Debt"
 lab var avg_ukus "Global Interest Rate"
lab var data_region "Regional Debt Data"
  
 
 estout a1 a2, cells(b(star fmt(%9.3f)) se(par fmt(%9.2f))) starlevels(* 0.1 ** 0.05 *** 0.01) ///
eqlab("/lnsig2u" "ln($\sigma^2_u$)", none) ///
stats( ll aic N, fmt(%9.1f %9.1f %9.0f) labels("Log-Likelihood" "AIC") star)  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) order() drop() ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4") ///
title("")
 

 *****Restructuring v Default
 
use "leaderdefault_041018.dta", clear
xtset ccode year

 
 xtlogit F.bhaircut2 irr_entry reg_leader     ///
oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
  estimates store re1

  xtlogit F.max_postdefault2 irr_entry reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode)
  estimates store re2
  




 xtlogit F.rrdef2  irr_entry reg_leader     ///
 oyrs o2 o3  reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2 if year >=1975 & year <=2014, i(ccode)
 estimates store re3
 
 
lab var irr_entry "Irregular Leader Change" 
 lab var reg_leader "Regular Leader Change" 
 lab var max_irregular "Irregular Leader" 
 lab var max_regular "Regular Leader" 
 lab var ten_irr2  "Tenure*Irregular Leader" 
 lab var  oyrs "Years since Default Onset"
 lab  var reg_all "\# of Defaults in Region"
 lab var systemiccrisis "Economic Crisis"
 lab var maddion_gdp "GDP"
 lab var egrowth "Growth"
 lab var debtch "Change in Debt"
 lab var avg_ukus "Global Interest Rate"
 lab var lnten2 "Tenure"
 
      estout re1 re2 re3, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
	eqlab("/lnsig2u" "ln($\sigma^2_u$)", none) ///
stats( ll aic N, fmt(%9.1f %9.1f %9.0f) labels("Log-Likelihood" "AIC"))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(irr_entry reg_leader dem2) drop(o2 o3  ) ///
mlabels ("Model 1" "Model 2" "Model 3" ) ///
title("")

 
 
 **Debt Forgivenesss
 use "leaderdefault_041018.dta", clear
xtset ccode year


xtlogit F.ext_onset2 irr_entry  reg_leader  reg_foregive   ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2 if year>=1990, i(ccode)
  est sto m1
 
 

 

 xtlogit F.ext_onset2 irr_entry  reg_leader   mean_for   ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2 if year>=1990, i(ccode)
   est sto m2
 
  

 
 gen HIPC = 1 if (ccode == 145 & year>=1998 & year <=2006) | (ccode == 434 & year>=2000 & year <=2006) ///
  | (ccode == 439 & year>=2000 & year <=2006)   | (ccode == 516 & year>=2005 & year <=2009)  | (ccode == 471 & year>=2000 & year <=2006) ///
   | (ccode == 482 & year>=2008 & year <=2009)    | (ccode == 483 & (year>=2001 & year <=2010|year==2015)) ///
      | (ccode == 490 & year>=2003 & year <=2010)  | (ccode == 484 & year>=2008 & year <=2010)   | (ccode == 437 & year>=2009 & year <=2012) ///
 | (ccode == 530 & year>=2001 & year <=2007)  | (ccode == 420 & year>=2001 & year <=2006) ///
  | (ccode == 452 & year>=2002& year <=2006) | (ccode == 438 & (year>=2001 & year <=2009 | year==2012)) ///
  | (ccode == 404 & year>=2001& year <=2010)   | (ccode == 110 & year>=1999& year <=2006) ///
  | (ccode == 41 & year>=2006& year <=2009)   | (ccode == 91 & year>=2001& year <=2006) ///
  | (ccode == 450 & year>=2008& year <=2010) | (ccode == 580 & year>=2001& year <=2006) ///
  | (ccode == 553 & year>=2001& year <=2006) | (ccode == 432 & year>=2000& year <=2006) ///
  | (ccode == 435 & year>=2000& year <=2006) | (ccode == 580 & year>=2000& year <=2006) ///
  | (ccode == 541 & year>=1999& year <=2006) | (ccode == 93 & year>=2002& year <=2006) ///
  | (ccode == 436 & year>=2001& year <=2006)  | (ccode == 517 & year>=2001& year <=2006) ///
  | (ccode == 403 & year==2007)  | (ccode == 433 & year>=2000& year <=2006) ///
   | (ccode == 451 & year>=2002& year <=2006)  | (ccode == 510 & year>=2000& year <=2006) ///
   | (ccode == 461 & year>=2008& year <=2010)  | (ccode == 500 & year>=1998& year <=2006) ///
| (ccode == 551 & year>=2001& year <=2006)

replace  HIPC = 0 if  HIPC ==. & year >=1998


 xtlogit F.ext_onset2 irr_entry  reg_leader  HIPC  ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2 if year>=1998, i(ccode)
 
 sort region year


*by region year: egen total_states = count(ccode)
 by region year: egen total_H = total(HIPC )
  replace total_H =  total_H - 1 if HIPC==1


  gen reg_HIPC = (total_H)/(total_states -1)
  
  
  xtset ccode year
 xtlogit F.ext_onset2 irr_entry  reg_leader reg_HIPC  ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2 if year>=1996, i(ccode)
   est sto m3
 
 
  gen HIPC2 = 1 if (ccode == 145 & year>=1998) | (ccode == 434 & year>=2000) ///
  | (ccode == 439 & year>=2000 )   | (ccode == 516 & year>=2005 )  | (ccode == 471 & year>=2000 ) ///
   | (ccode == 482 & year>=2008 )    | (ccode == 483 & year>=2001 )    ///
      | (ccode == 490 & year>=2003)   | (ccode == 484 & year>=2008  ) | (ccode == 437 & year>=2009) ///
 | (ccode == 530 & year>=2001 )  | (ccode == 420 & year>=2001) ///
  | (ccode == 452 & year>=2002 )| (ccode == 438 & year>=2001 ) ///
  | (ccode == 404 & year>=2001)   | (ccode == 110 & year>=1999) ///
  | (ccode == 41 & year>=2006)   | (ccode == 91 & year>=2001) ///
  | (ccode == 450 & year>=2008) | (ccode == 580 & year>=2001) ///
  | (ccode == 553 & year>=2001) | (ccode == 432 & year>=2000) ///
  | (ccode == 435 & year>=2000) | (ccode == 580 & year>=2000) ///
  | (ccode == 541 & year>=1999) | (ccode == 93 & year>=2002) ///
  | (ccode == 436 & year>=2001)  | (ccode == 517 & year>=2001) ///
  | (ccode == 403 & year>=2007)  | (ccode == 433 & year>=2000) ///
   | (ccode == 451 & year>=2002)  | (ccode == 510 & year>=2000) ///
   | (ccode == 461 & year>=2008)  | (ccode == 500 & year>=1998) ///
| (ccode == 551 & year>=2001)

replace  HIPC2 = 0 if  HIPC2 ==. & year >=1998

 sort region year


*by region year: egen total_states = count(ccode)
 by region year: egen total_H2 = total(HIPC2 )
  replace total_H2 =  total_H2 - 1 if HIPC2==1


  gen reg_HIPC2 = (total_H2)/(total_states -1)
  
  
    xtset ccode year
 xtlogit F.ext_onset2 irr_entry  reg_leader reg_HIPC2  ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2 if year>=1996, i(ccode)
   est sto m4
   
   
   lab var irr_entry "Irregular Leader Change" 
 lab var reg_leader "Regular Leader Change" 
 lab var max_irregular "Irregular Leader" 
 lab var max_regular "Regular Leader" 
 lab var ten_irr2  "Tenure*Irregular Leader" 
 lab var  oyrs "Years since Default Onset"
 lab  var reg_all "\# of Defaults in Region"
 lab var systemiccrisis "Economic Crisis"
 lab var maddion_gdp "GDP"
 lab var egrowth "Growth"
 lab var debtch "Change in Debt"
 lab var avg_ukus "Global Interest Rate"
 lab var reg_foregive "Regional Forgiveness"
  lab var mean_for "Regional Forgiveness (\%)"
 lab var reg_HIPC "Regional HIPC"
 lab var reg_HIPC2 "Regional HIPC (ongoing)"

 
      estout m1 m2 m3 m4, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
	eqlab("/lnsig2u" "ln($\sigma^2_u$)", none) ///
stats( ll aic N, fmt(%9.1f %9.1f %9.0f) labels("Log-Likelihood" "AIC"))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(irr_entry  reg_foregive mean_for reg_HIPC  reg_HIPC2 reg_leader dem2) drop(o2 o3  ) ///
mlabels ("Model 1" "Model 2" "Model 3" ) ///
title("")
   
   
**Additional Robustness Tests

			
			 
use "leaderdefault_041018.dta", clear

set more off

xtset ccode year

xtlogit F.ext_onset2 irr_entry reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode) fe
estimates store fe

xtlogit F.ext_onset2 irr_entry3 reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode) 
estimates store m2

xtlogit F.ext_onset2 irr_entry conflict reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode) 
estimates store m3

gen sol_irr = solschdum*irr_entry
gen sol_reg = solschdum*reg_leader

xtlogit F.ext_onset2 solschdum irr_entry  reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode) 
estimates store m4

xtlogit F.ext_onset2 solschdum irr_entry sol_irr reg_leader     ///
 oyrs o2 o3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode) 
estimates store m5


xtlogit F.default irr_entry  reg_leader     ///
 tyrs t2 t3 reg_all  systemiccrisis maddion_gdp ///
 egrowth debtGDP debtch avg_ukus dem2, i(ccode) 
estimates store m6

lab var irr_entry3 "Irr. Leader Change w/ FIRC"
lab var solschdum "SOLS Change"
lab var tyrs "Years since Default (Tomz)"
lab var sol_irr "Irr. Leader*SOLS Change"
 
      estout fe m2 m3 m4 m5 m6  , cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
	eqlab("/lnsig2u" "ln($\sigma^2_u$)", none) ///
stats( ll aic N, fmt(%9.1f %9.1f %9.0f) labels("Log-Likelihood" "AIC") star)  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(irr_entry irr_entry3 conflict solschdum sol_irr oyrs tyrs) drop(o2 o3 t2 t3 ) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4") ///
title("")


